package com.liuxinlong.modules.common.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.common.service.WorkFlowGroupService;
import com.liuxinlong.modules.dao.UserDao;
import com.liuxinlong.modules.dao.WorkFlowGroupDao;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.modules.entity.WorkFlowGroup;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程用户组接口实现
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-05-22
 */
@Service
@Slf4j
public class WorkFlowGroupServiceImpl implements WorkFlowGroupService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private WorkFlowGroupDao workFlowGroupDao;

    @Override
    public List<Map<String, Object>> queryWorkflowGroupList(Map<String, Object> queryParam) {
        List<WorkFlowGroup> originalList = workFlowGroupDao.pageWorkflowGroupList(queryParam);
        List<Map<String, Object>> dataList = new ArrayList<>(originalList.size());
        Map<String, Map<String, String>> userMap = userDao.getUserMap();
        int sort = (int) queryParam.get("startNum") + 1;
        for (WorkFlowGroup group : originalList) {
            Map<String, Object> item = ObjectUtils.object2Map(group);
            item.put("sort", sort);
            List<String> userName = new ArrayList<>();
            if (!StringUtils.isEmpty(group.getGroupUser())) {
                List<String> userList = Arrays.asList(group.getGroupUser().split(","));
                for (String user : userList) {
                    userName.add(userMap.containsKey(user) ? userMap.get(user).get("name") : "");
                }
            }
            item.put("userName", StringUtils.strip(userName.toString(), "[]"));
            item.put("modify", group.getId().length() > 1);
            dataList.add(item);
            sort++;
        }
        return dataList;
    }

    @Override
    public int getWorkflowGroupCount(Map<String, Object> queryParam) {
        return workFlowGroupDao.getWorkflowGroupCount(queryParam);
    }

    @Override
    public void addWorkflowGroup(WorkFlowGroup workFlowGroup) {
        Map<String, Object> query = new HashMap<>();
        query.put("groupSn", workFlowGroup.getGroupSn());
        int existNum = workFlowGroupDao.getWorkflowGroupCount(query);
        if (existNum > 0) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "新增失败，已存在相同用户组编码！");
        }
        workFlowGroup.setId(SnowFlake.nextIdStr());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        workFlowGroup.setCreateTime(currentTime);
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        workFlowGroup.setCreateUser(currentUser.getId());
        workFlowGroupDao.insert(workFlowGroup);
    }

    @Override
    public void updateWorkflowGroup(WorkFlowGroup workFlowGroup) {
        if (workFlowGroup.getId().length() < 2) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "系统配置用户组，不可修改！");
        }
        Map<String, Object> query = new HashMap<>();
        query.put("groupSn", workFlowGroup.getGroupSn());
        query.put("startNum", 0);
        query.put("pageSize", 10);
        List<WorkFlowGroup> dbGroupList = workFlowGroupDao.pageWorkflowGroupList(query);
        if (!dbGroupList.isEmpty() && !StringUtils.equals(workFlowGroup.getId(), dbGroupList.get(0).getId())) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "修改失败，已存在相同用户组编码！");
        }
        WorkFlowGroup oldData = workFlowGroupDao.selectById(workFlowGroup.getId());
        if (ObjectUtils.isEmpty(oldData)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "修改失败，原信息不存在！");
        }
        workFlowGroup.setCreateUser(oldData.getCreateUser());
        workFlowGroup.setCreateTime(oldData.getCreateTime());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        workFlowGroup.setUpdateTime(currentTime);
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        workFlowGroup.setUpdateUser(currentUser.getId());
        workFlowGroupDao.updateById(workFlowGroup);
    }

    @Override
    public void deleteWorkflowGroup(String id) {
        if (id.length() < 2) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "系统配置用户组，不可删除！");
        }
        workFlowGroupDao.deleteById(id);
    }

}
